package cn.group.controller;

import cn.group.domain.SC;
import cn.group.domain.TeachInfo;
import cn.group.domain.Teacher;
import cn.group.service.TeachInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teachInfo")
public class TeachInfoController {
    @Autowired
    private TeachInfoService teachInfoService;

    @RequestMapping("/showTeachInfo")
    public String getTeachInfoList(){
        return "TeachInfo";
    }
//加载刷新
    @RequestMapping("/getteachInfo")
    @ResponseBody
    public Map<String,Object> getTeachInfo(){
        List<TeachInfo> teachInfos = teachInfoService.getAll();
        System.out.println(teachInfos);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取授课信息成功！");
        result.put("teachInfoList",teachInfos);
        return result;
    }
    @RequestMapping("/getListBypage")
    @ResponseBody
    public Map<String,Object>getListBypage(Integer currentPage,Integer pageSize){
        PageInfo<TeachInfo> pageInfo = teachInfoService.getTeachInfoByPage(currentPage,pageSize);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取授课信息成功");
        result.put("teachInfoList",pageInfo.getList());//返回查询出来的分页数据
        for (TeachInfo info : pageInfo.getList()) {
            System.out.println(info);
        }
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }
    //拿到一条数据信息
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String,Object> getone(Integer id){
        Map<String,Object> result=new HashMap<>();
        TeachInfo teachInfo =teachInfoService.getOneById(id);
        result.put("code",0);
        result.put("message","获取授课信息成功！");
        result.put("teachInfoList",teachInfo);
        return result;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object>edit(TeachInfo teachInfo){
        Map<String,Object>result = new HashMap<>();
        boolean modified;
        if (teachInfo.getId()>0){
            modified = teachInfoService.modifyOne(teachInfo);
        }else {
            modified = teachInfoService.addOne(teachInfo);
        }
        if (!modified){
            result.put("code",-1);
            result.put("message","编辑授课信息失败");
        }else {
            result.put("code",0);
            result.put("message","编辑授课信息成功");
        }
        return result;
    }
}
